package object_class;

//法一：重写compareTo，toString，利用Arrays下的sort，toString（nwam排序）
import java.util.Arrays;

class Person implements Comparable{
    private String name;
    private int age;

    public Person(String name,int age) {
        this.name=name;
        this.age=age;
    }

    //重写Comparable中抽象方法
    @Override
    public int compareTo(Object object) {
        if(object instanceof Person) {
            Person person = (Person) object;
            if(this.name.compareTo(person.name)>0) { //compareTo系统帮我们重写了
                return -1;
            }else if(this.name.compareTo(person.name)<0) {
                return 1;
            }else {
                return 0;
            }
        }
        return 0;
    }

    //重写toString,和Arrays下的toString配合使用
    @Override
    public String toString() { //如果不重写toString,返回值是一个地址
        return "["+this.name+this.age+"]";
    }
}
public class SortTest {
    //我们自己重新实现sort
    public static void sort(Person[] peoples) {
        for (int i = 0; i < peoples.length-1; i++) {
            for (int j = 0; j < peoples.length-1-i; j++) {
                if(peoples[j].compareTo(peoples[j+1])>0) {
                    Person tmp = peoples[j];
                    peoples[j] = peoples[j+1];
                    peoples[j+1] = tmp;
                }
            }
        }
    }
    public static void main(String[] args) {
        Person[] peoples = {
                new Person("zhangsan",19),
                new Person("wangwu",17),
                new Person("alan",21)
        };
        Arrays.sort(peoples);
        System.out.println(Arrays.toString(peoples));
    }
}


//法二：重写compareTo，toString，在测试类中重新实现sort，利用Arrays下的toString（age排序）
package object_class;

import java.util.Arrays;

class Person implements Comparable{
    private String name;
    private int age;

    public Person(String name,int age) {
        this.name=name;
        this.age=age;
    }

    //重写Comparable中抽象方法
    @Override
    public int compareTo(Object object) {
        if(object instanceof Person) {
            Person person = (Person) object;
            if(this.age>person.age) { //compareTo系统帮我们重写了
                return -1;
            }else if(this.age<person.age) {
                return 1;
            }else {
                return 0;
            }
        }
        return 0;
    }

    //重写toString,和Arrays下的toString配合使用
    @Override
    public String toString() { //如果不重写toString,返回值是一个地址
        return "["+this.name+this.age+"]";
    }
}
//测试类
public class SortTest {
    public static void main(String[] args) {
        Person[] peoples = {
                new Person("zhangsan",19),
                new Person("wangwu",17),
                new Person("alan",21)
        };
        Arrays.sort(peoples);
        System.out.println(Arrays.toString(peoples));
    }
}


//法二：重写compareTo，toString，在测试类中重新实现sort，利用Arrays下的toString（age排序）
package object_class;

import java.util.Arrays;

class Person implements Comparable{
    private String name;
    private int age;

    public Person(String name,int age) {
        this.name=name;
        this.age=age;
    }

    //重写Comparable中抽象方法
    @Override
    public int compareTo(Object object) {
        if(object instanceof Person) {
            Person person = (Person) object;
            if(this.age>person.age) { //compareTo系统帮我们重写了
                return -1;
            }else if(this.age<person.age) {
                return 1;
            }else {
                return 0;
            }
        }
        return 0;
    }

    //重写toString,和Arrays下的toString配合使用
    @Override
    public String toString() { //如果不重写toString,返回值是一个地址
        return "["+this.name+this.age+"]";
    }
}
//测试类
public class SortTest {
    public static void main(String[] args) {
        Person[] peoples = {
                new Person("zhangsan",19),
                new Person("wangwu",17),
                new Person("alan",21)
        };
        Arrays.sort(peoples);
        System.out.println(Arrays.toString(peoples));
    }
}